﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DatabaseConnectionFactory.cs" company="">
// </copyright>
// <summary>
//   Factory that creates the appropriate database connection.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace UITest.Helpers.DataAccess
{
    using System;
    using System.Configuration;
    using System.Data.Common;
    using System.Globalization;

    /// <summary>
    ///     Factory that creates the appropriate database connection.
    /// </summary>
    public static class DatabaseConnectionFactory
    {
        #region Public Methods and Operators

        /// <summary>
        /// Creates the default connection.
        /// </summary>
        /// <param name="connectionName">
        /// Name of the connection.
        /// </param>
        /// <returns>
        /// An instance of the <see cref="DbConnection"/>.
        /// </returns>
        public static DbConnection CreateConnection(string connectionName)
        {
            ConnectionStringSettings defaultConnectionStringSettings =
                ConfigurationManager.ConnectionStrings[connectionName];

            if (defaultConnectionStringSettings == null)
            {
                throw new ConfigurationErrorsException("Could not found the default connection string.");
            }

            DbProviderFactory dbProviderFactory =
                DbProviderFactories.GetFactory(defaultConnectionStringSettings.ProviderName);

            if (dbProviderFactory == null)
            {
                throw new NotSupportedException(
                    string.Format(
                        CultureInfo.InvariantCulture, 
                        "The database provider {0} is not supported", 
                        defaultConnectionStringSettings.ProviderName));
            }

            DbConnection connection = dbProviderFactory.CreateConnection();

            if (connection == null)
            {
                throw new InvalidOperationException("Could not create the connection.");
            }

            connection.ConnectionString = defaultConnectionStringSettings.ConnectionString;

            return connection;
        }

        /// <summary>
        /// The create connection.
        /// </summary>
        /// <param name="connectionString">
        /// The connection string.
        /// </param>
        /// <param name="providerName">
        /// The provider name.
        /// </param>
        /// <returns>
        /// The <see cref="DbConnection"/>.
        /// </returns>
        /// <exception cref="ConfigurationErrorsException">
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// </exception>
        public static DbConnection CreateConnection(string connectionString, string providerName)
        {
            string defaultConnectionStringSettings = connectionString;

            if (defaultConnectionStringSettings == null)
            {
                throw new ConfigurationErrorsException("Could not found the default connection string.");
            }

            DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory(providerName);

            if (dbProviderFactory == null)
            {
                throw new NotSupportedException(
                    string.Format(
                        CultureInfo.InvariantCulture, 
                        "The database provider {0} is not supported", 
                        providerName));
            }

            DbConnection connection = dbProviderFactory.CreateConnection();

            if (connection == null)
            {
                throw new InvalidOperationException("Could not create the connection.");
            }

            connection.ConnectionString = defaultConnectionStringSettings;

            return connection;
        }

        #endregion
    }
}